package io.shuttle.mbe.api

import io.shuttle.mbe.api.annotation.ChromeMinVersion
import io.shuttle.mbe.api.annotation.PromiseStyleMinVersion
import io.shuttle.mbe.api.types.ImageData
import io.shuttle.mbe.api.types.Value1Function
import io.shuttle.mbe.api.types.VoidFunction
import io.shuttle.mbe.core.Promise

////////////////////
// Page Action
////////////////////
/**
 * Use the `Browser.pageAction` API to put icons in the main Google Chrome toolbar, to the right of the address bar. Page actions represent actions that can be taken on the current page, but that aren't applicable to all pages. Page actions appear grayed out when inactive.
 *
 * Manifest: "page_action"
 *
 * MV2 only
 */
interface PageAction {
    // Gets the html document set as the popup for this page action.
    // @return {callback.result}
    @PromiseStyleMinVersion(101)
    fun getPopup(
        details: TabDetails,
        callback: Value1Function<String>? = null
    ): Promise<String>

    // Gets the title of the page action.
    @PromiseStyleMinVersion(101)
    fun getTitle(
        details: TabDetails,
        callback: Value1Function<String>? = null
    ): Promise<String>

    // Hides the page action. Hidden page actions still appear in the Chrome toolbar, but are grayed out.
    @PromiseStyleMinVersion(101)
    fun hide(
        tabId: Number,
        @ChromeMinVersion(67)
        callback: VoidFunction? = null
    ): Promise<Void>

    // Sets the icon for the page action.
    // The icon can be specified either as the path to an image file or as the pixel data from a canvas element,
    // or as dictionary of either one of those.
    // Either the path or the imageData property must be specified.
    @PromiseStyleMinVersion(101)
    fun setIcon(
        details: IconDetails,
        callback: VoidFunction? = null
    ): Promise<Void>

    // Sets the HTML document to be opened as a popup when the user clicks on the page action's icon.
    @PromiseStyleMinVersion(101)
    fun setPopup(
        details: PopupDetails,
        @ChromeMinVersion(67)
        callback: VoidFunction? = null
    ): Promise<Void>

    // Sets the title of the page action. This is displayed in a tooltip over the page action.
    @PromiseStyleMinVersion(101)
    fun setTitle(
        details: TitleDetails,
        @ChromeMinVersion(67)
        callback: VoidFunction? = null
    ): Promise<Void>

    // Shows the page action. The page action is shown whenever the tab is selected.
    @PromiseStyleMinVersion(101)
    fun show(
        // The id of the tab for which you want to modify the page action.
        tabId: Number,
        @ChromeMinVersion(67)
        callback: VoidFunction? = null
    ): Promise<Void>

    // Fired when a page action icon is clicked. This event will not fire if the page action has a popup.
    val onClicked: Events.Event<Value1Function<Tabs.Tab>>


    @ChromeMinVersion(88)
    data class TabDetails(
        // The ID of the tab to query state for.
        // If no tab is specified, the non-tab-specific state is returned.
        var tabId: Number?,
    )

    data class IconDetails(
        // Deprecated. This argument is ignored.
        @Deprecated("Deprecated. This argument is ignored.")
        var iconIndex: Number?,
        // Either an ImageData object or a dictionary {size -> ImageData} representing icon to be set.
        // If the icon is specified as a dictionary,
        // the actual image to be used is chosen depending on screen's pixel density.
        // If the number of image pixels that fit into one screen space unit equals scale,
        // then image with size scale * n will be selected, where n is the size of the icon in the UI.
        // At least one image must be specified.
        // Note that 'details.imageData = foo' is equivalent to 'details.imageData = {'16': foo}'
        // FIXME: or Any
        var imageData: ImageData?,
        // Either a relative image path or a dictionary {size -> relative image path} pointing to icon to be set.
        // If the icon is specified as a dictionary,
        // the actual image to be used is chosen depending on screen's pixel density.
        // If the number of image pixels that fit into one screen space unit equals scale,
        // then image with size scale * n will be selected, where n is the size of the icon in the UI.
        // At least one image must be specified.
        // Note that 'details.path = foo' is equivalent to 'details.path = {'16': foo}'
        // FIXME: or Any
        var path: String?,
        // The id of the tab for which you want to modify the page action.
        var tabId: Number?,
    )

    data class PopupDetails(
        // The relative path to the HTML file to show in a popup. If set to the empty string (''), no popup is shown.
        var popup: String,
        // The id of the tab for which you want to modify the page action.
        var tabId: Number?,
    )

    data class TitleDetails(
        // The tooltip string.
        var title: String,
        // The id of the tab for which you want to modify the page action.
        var tabId: Number?,
    )
}